const { sequelize } = require('../../core/db')
const { Sequelize, Model, Op } = require('sequelize');
const { Favor } = require('./favorModel');

class HotBook extends Model {
  static async getAll() {
    const books = await HotBook.findAll({
      order:[
        'index'
      ]
    })
    const ids = []
    books.forEach( (book) => {
      ids.push(book.id)
    });
    const favors = Favor.findAll({
      where: {
        art_id: {
          [Op.in]:ids
        }
        
      },
      group: ['art_id'],
      attributes: ['art_id',[Sequelize.fn('COUNT', '*'), 'count']] // 查询出来的接口集合需要哪些字段

    })
    //Sequelize.fn('COUNT') Sequelize.fn('SUM')
    return favors
  }
  
}


HotBook.init({
  index: Sequelize.INTEGER,
  image: Sequelize.STRING,
  author: Sequelize.STRING,
  title: Sequelize.STRING,
}, {
  sequelize,
  tableName: 'hotBook'
})

module.exports = {
  HotBook
}